package com.dgut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.constant.MessageConstant;
import com.dgut.dto.CustomerPageDTO;
import com.dgut.entity.Customers;
import com.dgut.exception.RequireParameterException;
import com.dgut.mapper.CustomerMapper;
import com.dgut.result.PageResult;
import com.dgut.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 分页查询客户信息
     *
     * @param customerPageDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CustomerPageDTO customerPageDTO) {
        if (customerPageDTO == null) {
            throw new RequireParameterException(MessageConstant.REQUIRED_PARAMETER_IS_NULL);
        }
        // 分页查询
        Page<Customers> page = new Page<>(customerPageDTO.getCurrentPage(), customerPageDTO.getPageSize());
        // 创建查询条件
        QueryWrapper<Customers> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(customerPageDTO.getCusName() != null && customerPageDTO.getCusName().length() > 0, "cus_name", customerPageDTO.getCusName())
                .like(customerPageDTO.getCity() != null && customerPageDTO.getCity().length() > 0, "city", customerPageDTO.getCity());

        Page<Customers> customersPage = customerMapper.selectPage(page, queryWrapper);

        return new PageResult(customersPage.getTotal(), customersPage.getRecords());
    }

    /**
     * 查询客户数量
     *
     * @return
     */
    @Override
    public Long count() {
        Long count = customerMapper.selectCount(null);
        return count;
    }

    /**
     * 新增客户
     *
     * @param customers
     */
    @Override
    public void save(Customers customers) {
        if (customers == null) {
            throw new RequireParameterException(MessageConstant.REQUIRED_PARAMETER_IS_NULL);
        }
        customerMapper.insert(customers);
    }

    /**
     * 删除客户
     *
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        customerMapper.deleteBatchIds(ids);
    }

    /**
     * 修改客户
     *
     * @param customers
     */
    @Override
    public void update(Customers customers) {
        if (customers == null) {
            throw new RequireParameterException(MessageConstant.REQUIRED_PARAMETER_IS_NULL);
        }
        UpdateWrapper<Customers> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("cus_id", customers.getCusId());
        customerMapper.update(customers, updateWrapper);
    }

    /**
     * 查询所有客户
     *
     * @return
     */
    @Override
    public List<Customers> list() {
        List<Customers> list = customerMapper.selectList(null);
        return list;
    }
}
